﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.DAL;
using WXYW.Entities;
using WXYW.Model;

namespace WXYW.BLL
{
    public class ZBTJService : RepositoryBase
    {
        /// <summary>
        /// 0-5 TAT 当月指标
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public int Get0ToXCountTATCountOfCurrentMonth(int x)
        {
            int totalCount = GetTotalCompleteOfCurrentMonth(DateTime.Now.Year, DateTime.Now.Month);
            int sXCount = GetSXCountOfMonth(DateTime.Now.Year, DateTime.Now.Month, x);
            if (totalCount > 0)
            {
                return sXCount * 100 / totalCount;
            }
            return 0;
        }

        /// <summary>
        /// 获取季度0-5TAT 指标
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetQ0T5TAT(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0} Q1", year);
                q1Model.Month = "Q1";
                q1Model.ZB = Get0T5TATOfTimeRange(year, 4, year, 6);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0} Q2", year);
                q2Model.Month = "Q2";
                q2Model.ZB = Get0T5TATOfTimeRange(year, 7, year, 9);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0} Q3", year);
                q3Model.Month = "Q3";
                q3Model.ZB = Get0T5TATOfTimeRange(year, 10, year, 12);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0} Q4", year);
                q4Model.Month = "Q4";
                q4Model.ZB = Get0T5TATOfTimeRange(year+1, 1, year+1, 3);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        public int Get0T5TATOfTimeRange(int startYear, int startMonth, int endYear, int endMonth)
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }

            DateTime endDate = new DateTime(endYear, endMonth, 26);
            DateTime endDate1 = new DateTime(endYear, endMonth, 28);
            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p =>
                p.ApplicationForm.User.ServiceStation == null &&
                p.DistributeDateTime.HasValue &&
                p.RepairCompleteDateTime.Value > startDate &&
                p.RepairCompleteDateTime.Value < endDate1);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (!application.IsDecideToRepair.HasValue || application.IsDecideToRepair.Value == true)
                {
                    if (!string.IsNullOrEmpty(application.SendWay))
                    {
                        if (application.SendWay.Trim().Equals("A") &&
                        application.NoticeClientDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.NoticeClientDateTime.Value);
                        }
                        else if (application.SendWay.Trim().StartsWith("B") &&
                        application.ClientReceiveDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.ClientReceiveDateTime.Value);
                        }
                    }

                    int quoteTimeSpan = 0;
                    if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                    {
                        quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                    }

                    if (quoteTimeSpan <= timeSpan)
                    {
                        timeSpan = timeSpan - quoteTimeSpan - 1;
                        if (timeSpan <= 5)
                        {
                            count++;
                        }
                    }
                    else
                    {
                        if (application.QuotationDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.QuotationDateTime.Value);
                            if (timeSpan <= 5)
                            {
                                count++;
                            }
                        }
                    }
                }
                else if (application.IsDecideToRepair.HasValue && application.IsDecideToRepair.Value == false)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.RepairCompleteDateTime.Value);
                    if (timeSpan <= 5)
                    {
                        count++;
                    }
                }
            }
            if (applications.Count() > 0)
            {
                return count*100 / applications.Count();
            }

            return 0;
        }

        public int Get0T3TATOfTimeRange(int startYear, int startMonth, int endYear, int endMonth)
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }

            DateTime endDate = new DateTime(endYear, endMonth, 26);
            DateTime endDate1 = new DateTime(endYear, endMonth, 28);
            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => 
                p.ApplicationForm.User.ServiceStation == null &&
                p.ProductRepairType.Equals("保内") &&
                p.DistributeDateTime.HasValue &&
                p.RepairCompleteDateTime.Value > startDate && 
                p.RepairCompleteDateTime.Value < endDate1);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (!application.IsDecideToRepair.HasValue || application.IsDecideToRepair.Value == true)
                {
                    if (!string.IsNullOrEmpty(application.SendWay))
                    {
                        if (application.SendWay.Trim().Equals("A") &&
                        application.NoticeClientDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.NoticeClientDateTime.Value);
                        }
                        else if (application.SendWay.Trim().StartsWith("B") &&
                        application.ClientReceiveDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.ClientReceiveDateTime.Value);
                        }
                    }

                    int quoteTimeSpan = 0;
                    if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                    {
                        quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                    }

                    if (quoteTimeSpan <= timeSpan)
                    {
                        timeSpan = timeSpan - quoteTimeSpan - 1;
                        if (timeSpan <= 3)
                        {
                            count++;
                        }
                    }
                    else
                    {
                        if (application.QuotationDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.QuotationDateTime.Value);
                            if (timeSpan <= 3)
                            {
                                count++;
                            }
                        }
                    }
                }
                else if (application.IsDecideToRepair.HasValue && application.IsDecideToRepair.Value == false)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.RepairCompleteDateTime.Value);
                    if (timeSpan <= 3)
                    {
                        count++;
                    }
                }
            }
            if (applications.Count() > 0)
            {
                return count * 100 / applications.Count();
            }

            return 0;
        }

        public int Get0To3CountOfCurrentMonth()
        {
            int totalCount = GetTotalBNCompleteCount(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Month);
            int sXCount = GetS3Count(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Month);
            if (totalCount > 0)
            {
                return sXCount * 100 / totalCount;
            }
            return 0;
        }

        /// <summary>
        /// 朱经理(1049776210) 15:37:52 PM 
        /// 完修量 =1月26日到2月27日维修完毕的数量
        /// 朱经理(1049776210) 15:39:45 PM 
        /// 但是派单截止到25日
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public int GetTotalCompleteOfCurrentMonth(int year, int month) // 完修总量
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 25);
            }

            DateTime endDate = new DateTime(year, month, 26);
            DateTime endDate1 = new DateTime(year, month, 28);
            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => 
                p.ApplicationForm.User.ServiceStation == null &&
                p.DistributeDateTime.HasValue && 
                p.RepairCompleteDateTime.HasValue &&
                p.RepairCompleteDateTime.Value > startDate &&
                p.RepairCompleteDateTime.Value < endDate1);
            return applications.Count();            
        }

        public int GetTotalBNCompleteCount(int startYear, int startMonth, int endYear, int endMonth) // 保内完修总量
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);
            DateTime endDate1 = new DateTime(endYear, endMonth, 28);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => 
                p.ApplicationForm.User.ServiceStation == null &&
                p.ProductRepairType.Equals("保内") &&
                p.DistributeDateTime.HasValue &&
                p.RepairCompleteDateTime > startDate &&
                p.RepairCompleteDateTime < endDate1);
            return applications.Count();
        }

        public int GetSXCountOfMonth(int year, int month, int x)
        {
            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 25);
            }

            DateTime endDate = new DateTime(year, month, 26);
            DateTime endDate1 = new DateTime(year, month, 28);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p => 
                p.ApplicationForm.User.ServiceStation == null &&
                p.DistributeDateTime.HasValue &&
                p.RepairCompleteDateTime.Value > startDate &&
                p.RepairCompleteDateTime.Value < endDate1);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (!application.IsDecideToRepair.HasValue || application.IsDecideToRepair.Value == true)
                {
                    if (!string.IsNullOrEmpty(application.SendWay))
                    {
                        if (application.SendWay.Trim().Equals("A") &&
                        application.NoticeClientDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.NoticeClientDateTime.Value);
                        }
                        else if (application.SendWay.Trim().StartsWith("B") &&
                        application.ClientReceiveDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.ClientReceiveDateTime.Value);
                        }
                    }

                    int quoteTimeSpan = 0;
                    if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                    {
                        quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                    }

                    if (quoteTimeSpan <= timeSpan)
                    {
                        timeSpan = timeSpan - quoteTimeSpan - 1;
                        if (timeSpan <= x)
                        {
                            count++;
                        }
                    }
                    else
                    {
                        if (application.QuotationDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.QuotationDateTime.Value);
                            if (timeSpan <= x)
                            {
                                count++;
                            }
                        }
                    }
                }
                else if (application.IsDecideToRepair.HasValue && application.IsDecideToRepair.Value == false)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.RepairCompleteDateTime.Value);
                    if (timeSpan <= x)
                    {
                        count++;
                    }
                }
            }
            return count;
        }

        public int GetS3Count(int startYear, int startMonth, int endYear, int endMonth) // 保内0-3 TAT
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);
            DateTime endDate1 = new DateTime(endYear, endMonth, 28);

            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p =>
                p.ApplicationForm.User.ServiceStation == null &&
                p.ProductRepairType.Equals("保内") &&
                p.DistributeDateTime.HasValue &&
                p.RepairCompleteDateTime.Value > startDate &&
                p.RepairCompleteDateTime.Value < endDate1);

            int count = 0;
            foreach (ApplicationProduct application in applications)
            {
                if (!application.ApplicationFormReference.IsLoaded)
                {
                    application.ApplicationFormReference.Load();
                }

                int timeSpan = 0;
                if (!application.IsDecideToRepair.HasValue || application.IsDecideToRepair.Value == true)
                {
                    if (!string.IsNullOrEmpty(application.SendWay))
                    {
                        if (application.SendWay.Trim().Equals("A") &&
                        application.NoticeClientDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.NoticeClientDateTime.Value);
                        }
                        else if (application.SendWay.Trim().StartsWith("B") &&
                        application.ClientReceiveDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.ClientReceiveDateTime.Value);
                        }
                    }

                    int quoteTimeSpan = 0;
                    if (application.QuotationDateTime.HasValue && application.ConfirmQuotationDateTime.HasValue)
                    {
                        quoteTimeSpan = DateTimeService.CalculateDateTimeSpan(application.QuotationDateTime.Value, application.ConfirmQuotationDateTime.Value);
                    }

                    if (quoteTimeSpan <= timeSpan)
                    {
                        timeSpan = timeSpan - quoteTimeSpan - 1;
                        if (timeSpan <= 3)
                        {
                            count++;
                        }
                    }
                    else
                    {
                        if (application.QuotationDateTime.HasValue)
                        {
                            timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.QuotationDateTime.Value);
                            if (timeSpan <= 3)
                            {
                                count++;
                            }
                        }
                    }
                }
                else if (application.IsDecideToRepair.HasValue && application.IsDecideToRepair.Value == false)
                {
                    timeSpan = DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.RepairCompleteDateTime.Value);
                    if (timeSpan <= 3)
                    {
                        count++;
                    }
                }
            }
            return count;
        }        

        /// <summary>
        /// 当月完修率
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public int GetDYWXL()
        {
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;

            DateTime startDate = DateTime.Now;
            if (month > 1)
            {
                startDate = new DateTime(year, month - 1, 25);
            }
            else
            {
                startDate = new DateTime(year - 1, 12, 25);
            }

            DateTime endDate = new DateTime(year, month, 26);
            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p =>
                p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate && 
                p.ApplicationForm.ReceiveDateTime.Value < endDate);
            int bdzs =  applications.Count();// 派单总数

            DateTime endDate1 = new DateTime(year, month, 28);
            IQueryable<ApplicationProduct> applications1 = GetEntities<ApplicationProduct>().Where(p => p.DistributeDateTime.HasValue &&
               p.ApplicationForm.ReceiveDateTime.HasValue &&
               p.ApplicationForm.ReceiveDateTime.Value > startDate &&
               p.ApplicationForm.ReceiveDateTime.Value < endDate &&
               ((p.RepairCompleteDateTime.HasValue && p.RepairCompleteDateTime.Value <endDate1) ||
                p.Status.Equals("待付款确认")));
            int wxzs = applications1.Count(); // 完修总数
            if (wxzs > 0)
            {
                return wxzs * 100 / bdzs;
            }
            else
            {
                return 0;
            }
        }

        public List<ZBTJModel> Get0TXTAT(int startYear, int startMonth, int endYear, int endMonth, int x)
        {
            List<ZBTJModel> list = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    int totalCount = GetTotalCompleteOfCurrentMonth(year, month);
                    int sXCount = GetSXCountOfMonth(year, month, x);
                    if (totalCount > 0)
                    {
                       model.ZB = sXCount * 100 / totalCount;
                    }
                    model.Name = string.Format("{0}年{1}月", year, month);
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取区间内0-3TAT
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="startMonth"></param>
        /// <param name="endYear"></param>
        /// <param name="endMonth"></param>
        /// <returns></returns>
        public List<ZBTJModel> Get0T3TAT(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> list = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    int totalCount = GetTotalBNCompleteCount(year, month, year, month);
                    int sXCount = GetS3Count(year, month, year, month);
                    if (totalCount > 0)
                    {
                        model.ZB = sXCount * 100 / totalCount;
                    }
                    model.Name = string.Format("{0}年{1}月", year, month);
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取季度0-3TAT 指标
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetQ0T3TAT(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0} Q1", year);
                q1Model.Month = "Q1";
                q1Model.ZB = Get0T3TATOfTimeRange(year, 4, year, 6);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0} Q2", year);
                q2Model.Month = "Q2";
                q2Model.ZB = Get0T3TATOfTimeRange(year, 7, year, 9);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0} Q3", year);
                q3Model.Month = "Q3";
                q3Model.ZB = Get0T3TATOfTimeRange(year, 10, year, 12);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0} Q4", year);
                q4Model.Month = "Q4";
                q4Model.ZB = Get0T3TATOfTimeRange(year + 1, 1, year + 1, 3);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        public List<YSTJModel> GetYSTJ(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<YSTJModel> list = new List<YSTJModel>();
            YSTJModel totalYS = new YSTJModel(); // 总用时
            totalYS.ItemName = "总用时";

            YSTJModel averageYS = new YSTJModel(); // 平均用时
            averageYS.ItemName = "平均用时";

            List<double> fczq = GetFCZQ(startYear, startMonth, endYear, endMonth);
            totalYS.FC = fczq[0].ToString();
            averageYS.FC = fczq[1].ToString();

            List<double> bjZQ = GetBJSQZQ(startYear, startMonth, endYear, endMonth);
            totalYS.BJSQ = bjZQ[0].ToString();
            averageYS.BJSQ = bjZQ[1].ToString();

            List<double> sjZQ = GetSJZQ(startYear, startMonth, endYear, endMonth);
            totalYS.SJ = sjZQ[0].ToString();
            averageYS.SJ = sjZQ[1].ToString();

            List<double> wlPlatformToBJ = GetWLPlatformToBJ(startYear, startMonth, endYear, endMonth);
            totalYS.WLPlatformTBJ = wlPlatformToBJ[0].ToString();
            averageYS.WLPlatformTBJ = wlPlatformToBJ[1].ToString();

            List<double> wlCustomer = GetWLTOCustomer(startYear, startMonth, endYear, endMonth);
            totalYS.WLCustomer = wlCustomer[0].ToString();
            averageYS.WLCustomer = wlCustomer[1].ToString();

            List<double> wxZQ = GetWX(startYear, startMonth, endYear, endMonth);
            totalYS.WX = wxZQ[0].ToString();
            averageYS.WX = wxZQ[1].ToString();

            list.Add(totalYS);
            list.Add(averageYS);
            return list;
        }

        /// <summary>
        /// 返厂周期=收到工厂返回设备的日期-返厂发货日期
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        private List<double> GetFCZQ(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<double> list = new List<double>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);
            IQueryable<ReturnToFactoryForm> fcApplications = GetEntities<ReturnToFactoryForm>().Where(a=>
               a.ApplicationProduct.ApplicationForm.User.ServiceStation == null &&
               a.ApplyDate.HasValue && a.SendDate.HasValue &&
               a.ReceiveDate.HasValue && a.ApplyDate.Value > startDate && a.ApplyDate < endDate);
            int totalfcZQ = 0;
            foreach (ReturnToFactoryForm fcApplication in fcApplications)
            {
                totalfcZQ += DateTimeService.CalculateDateTimeSpan(fcApplication.SendDate.Value, fcApplication.ReceiveDate.Value);
            }
            list.Add(totalfcZQ);
            double avervateFCZQ = 0;
            if (fcApplications.Count() > 0)
            {
                avervateFCZQ = (double)totalfcZQ / (double)fcApplications.Count();
            }
            list.Add(Math.Round(avervateFCZQ,2));
            return list;
        }

        /// <summary>
        /// 备件申请周期（平台收到备件的日期-申请日期（如果没有批准不算））
        /// </summary>
        /// <returns></returns>
        private List<double> GetBJSQZQ(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<double> list = new List<double>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);
            IQueryable<SparePartApplyForm> bjApplications = GetEntities<SparePartApplyForm>(s=>
                s.ApplicationProduct.ApplicationForm.User.ServiceStation == null &&
                s.ApplyTime.HasValue && 
                s.ReceiveTime.HasValue &&
                s.ApplyTime > startDate && 
                s.ApplyTime < endDate);
            int totalBJZQ = 0;
            foreach (SparePartApplyForm bjApplication in bjApplications)
            {
                totalBJZQ += DateTimeService.CalculateDateTimeSpan(bjApplication.ApplyTime.Value, bjApplication.ReceiveTime.Value);
            }
            list.Add(totalBJZQ);
            double averageBJZQ = 0;
            if (bjApplications.Count() > 0)
            {
                averageBJZQ = (double)totalBJZQ / (double)bjApplications.Count();
            }
            list.Add(Math.Round(averageBJZQ,2));
            return list;
        }


        /// <summary>
        /// 升级周期（升级维修完毕发货日期或维修完毕发货日期-升级派单日期）
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="startMonth"></param>
        /// <param name="endYear"></param>
        /// <param name="endMonth"></param>
        /// <returns></returns>
        private List<double> GetSJZQ(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<double> list = new List<double>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            IQueryable<UpgradeForm> upgradeApplications = GetEntities<UpgradeForm>().Where(u => 
                u.ApplicationProduct.ApplicationForm.User.ServiceStation == null &&
                u.DistributeTime > startDate &&
                u.DistributeTime < endDate && 
                u.SendBackTime < endDate);
            int totalSJZQ = 0;
            foreach (UpgradeForm upgradeApplication in upgradeApplications)
            {
                totalSJZQ += DateTimeService.CalculateDateTimeSpan(upgradeApplication.DistributeTime.Value, upgradeApplication.SendBackTime.Value);
            }
            list.Add(totalSJZQ);

            double averageSJZQ = 0;
            if (upgradeApplications.Count() > 0)
            {
                averageSJZQ = (double)totalSJZQ / (double)upgradeApplications.Count();
            }
            list.Add(Math.Round(averageSJZQ,2));
            return list;
        }

        private List<double> GetWLPlatformToBJ(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<double> list = new List<double>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            IQueryable<UpgradeForm> upgradeApplications = GetEntities<UpgradeForm>().Where(u => 
                u.ApplicationProduct.ApplicationForm.User.ServiceStation == null &&
                u.SendTime > startDate &&
                u.SendTime < endDate && 
                u.ReceiveTime < endDate);
            int totalWLZQ = 0;
            foreach (UpgradeForm upgradeApplication in upgradeApplications)
            {
                totalWLZQ += DateTimeService.CalculateDateTimeSpan(upgradeApplication.SendTime.Value, upgradeApplication.ReceiveTime.Value);
            }
            list.Add(totalWLZQ);
            double averageWLZQ = 0;
            if (upgradeApplications.Count() > 0)
            {
                averageWLZQ = (double)totalWLZQ / (double)upgradeApplications.Count();
            }
            list.Add(Math.Round(averageWLZQ, 2));
            return list;
        }

        /// <summary>
        /// 物流北京-平台=升级维修完毕发货日期-平台升级收货日期
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="startMonth"></param>
        /// <param name="endYear"></param>
        /// <param name="endMonth"></param>
        /// <returns></returns>
        private List<double> GetWLBJToPlatform(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<double> list = new List<double>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            IQueryable<UpgradeForm> upgradeApplications = GetEntities<UpgradeForm>().Where(u => 
                u.ApplicationProduct.ApplicationForm.User.ServiceStation == null &&
                u.SendBackTime > startDate &&
                u.SendBackTime < endDate && u.SendBackTime < endDate);
            int totalWLZQ = 0;
            foreach (UpgradeForm upgradeApplication in upgradeApplications)
            {
                totalWLZQ += DateTimeService.CalculateDateTimeSpan(upgradeApplication.SendTime.Value, upgradeApplication.ReceiveTime.Value);
            }
            list.Add(totalWLZQ);
            double averageWLZQ = 0;
            if (upgradeApplications.Count() > 0)
            {
                averageWLZQ = (double)totalWLZQ / (double)upgradeApplications.Count();
            }
            list.Add(Math.Round(averageWLZQ, 2));
            return list;
        }

        /// <summary>
        /// 物流-客户=客户收货日期-各平台发货日期
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="startMonth"></param>
        /// <param name="endYear"></param>
        /// <param name="endMonth"></param>
        /// <returns></returns>
        private List<double> GetWLTOCustomer(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<double> list = new List<double>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            IQueryable<ApplicationForm> applications = GetEntities<ApplicationForm>().Where(a => 
                a.User.ServiceStation == null &&
                a.SendBackDateTime > startDate &&
                a.SendBackDateTime < endDate && 
                a.ClientReceiveDateTime < endDate);
            int totalWLZQ = 0;
            foreach (ApplicationForm application in applications)
            {
                totalWLZQ += DateTimeService.CalculateDateTimeSpan(application.SendBackDateTime.Value, application.ClientReceiveDateTime.Value);
            }
            list.Add(totalWLZQ);
            double averageWLZQ = 0;
            if (applications.Count() > 0)
            {
                averageWLZQ = (double)totalWLZQ / (double)applications.Count();
            }
            list.Add(Math.Round(averageWLZQ, 2));
            return list;
        }

        /// <summary>
        /// 工程师维修用时
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="startMonth"></param>
        /// <param name="endYear"></param>
        /// <param name="endMonth"></param>
        /// <returns></returns>
        private List<double> GetWX(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<double> list = new List<double>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);
            IQueryable<ApplicationProduct> applications = GetEntities<ApplicationProduct>().Where(p =>
                p.ApplicationForm.User.ServiceStation == null &&
                p.DistributeDateTime > startDate &&
                p.DistributeDateTime < endDate && 
                p.RepairCompleteDateTime < endDate);
            int totalWXZQ = 0;
            foreach (ApplicationProduct application in applications)
            {
                totalWXZQ += DateTimeService.CalculateDateTimeSpan(application.DistributeDateTime.Value, application.RepairCompleteDateTime.Value);
            }
            list.Add(totalWXZQ);
            double averageWXZQ = 0;
            if (applications.Count() > 0)
            {
                averageWXZQ = (double) totalWXZQ / (double)applications.Count();
            }
            list.Add(Math.Round(averageWXZQ,2));
            return list;
        }

        /// <summary>
        /// 获取完修率
        /// </summary>
        /// <returns></returns>
        private int GetWXL(int startYear, int startMonth, int endYear, int endMonth)
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);
            DateTime endDate1 = new DateTime(endYear, endMonth, 28);

            int totalCount = GetEntities<ApplicationProduct>().Where(p =>
                p.ApplicationForm.ReceiveDateTime.HasValue &&
                p.ApplicationForm.ReceiveDateTime.Value > startDate &&
                p.ApplicationForm.ReceiveDateTime.Value < endDate).Count();
            int completeCount = GetEntities<ApplicationProduct>().Where(p => p.DistributeDateTime.HasValue &&
               p.ApplicationForm.ReceiveDateTime.HasValue &&
               p.ApplicationForm.ReceiveDateTime.Value > startDate &&
               p.ApplicationForm.ReceiveDateTime.Value < endDate &&
               ((p.RepairCompleteDateTime.HasValue && p.RepairCompleteDateTime.Value < endDate1) ||
                p.Status.Equals("待付款确认"))).Count();
            if (totalCount > 0)
            {
                return completeCount*100 / totalCount;
            }
            else
            {
                return 0;
            }            
        }

        public List<ZBTJModel> GetWXLList(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> list = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    model.ZB = GetWXL(year, month, year, month);
                    model.Name = string.Format("{0}年{1}月", year, month);
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    list.Add(model);
                }
            }
            return list;
        }

        public List<ZBTJModel> GetQWXLList(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0} Q1", year);
                q1Model.Month = "Q1";
                q1Model.ZB = GetWXL(year, 4, year, 6);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0} Q2", year);
                q2Model.Month = "Q2";
                q2Model.ZB = GetWXL(year, 7, year, 9);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0} Q3", year);
                q3Model.Month = "Q3";
                q3Model.ZB = GetWXL(year, 10, year, 12);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0} Q4", year);
                q4Model.Month = "Q4";
                q4Model.ZB = GetWXL(year + 1, 1, year + 1, 3);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        /// <summary>
        /// 获取未完修比例分布
        /// </summary>
        /// <returns></returns>
        public List<IncompleteModel> GetIncompleteModelList(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<IncompleteModel> models = new List<IncompleteModel>();
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            int totalCount = GetEntities<ApplicationProduct>().Where(a => 
                a.ApplicationForm.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate).Count();

            // 待修单数
            int dxCount = GetEntities<ApplicationProduct>().Where(a => 
                a.ApplicationForm.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate && 
                !a.RepairCompleteDateTime.HasValue && 
                a.Status.Equals("待修")).Count();            

            // 备件申请单数
            int bjCount = GetEntities<ApplicationProduct>().Where(a => 
                a.ApplicationForm.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate && 
                !a.RepairCompleteDateTime.HasValue && 
                a.Status.Contains("备件")).Count();
            

            // 返厂单数
            int fcCount = GetEntities<ApplicationProduct>().Where(a => 
                a.ApplicationForm.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate && 
                !a.RepairCompleteDateTime.HasValue && 
                a.Status.Contains("返厂")).Count();
            

            // 升级单数
            int sjCount = GetEntities<ApplicationProduct>().Where(a => 
                a.ApplicationForm.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate && 
                !a.RepairCompleteDateTime.HasValue && 
                a.UpgradeForm.Count() > 0).Count();
            

            // 报价确认中
            int quoatationCount = GetEntities<ApplicationProduct>().Where(a => 
                a.ApplicationForm.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate && 
                !a.RepairCompleteDateTime.HasValue && 
                a.Status.Contains("报价确认中")).Count();

            int totalIncompleteCount = quoatationCount + dxCount + sjCount + fcCount + bjCount;

            IncompleteModel dxModel = new IncompleteModel();
            dxModel.ItemName = "待修";
            dxModel.Count = dxCount.ToString();
            dxModel.Percentage = totalCount > 0 ? (dxCount * 100 / totalIncompleteCount).ToString() : "0";
            models.Add(dxModel);

            IncompleteModel fcModel = new IncompleteModel();
            fcModel.ItemName = "待修-返厂";
            fcModel.Count = fcCount.ToString();
            fcModel.Percentage = totalCount > 0 ? (fcCount * 100 / totalIncompleteCount).ToString() : "0";
            models.Add(fcModel);

            IncompleteModel bjModel = new IncompleteModel();
            bjModel.ItemName = "待修-申请备件";
            bjModel.Count = bjCount.ToString();
            bjModel.Percentage = totalCount > 0 ? (bjCount * 100 / totalIncompleteCount).ToString() : "0";
            models.Add(bjModel);

            IncompleteModel sjModel = new IncompleteModel();
            sjModel.ItemName = "待修-升级";
            sjModel.Count = sjCount.ToString();
            sjModel.Percentage = totalCount > 0 ? (sjCount * 100 / totalIncompleteCount).ToString() : "0";
            models.Add(sjModel);

            IncompleteModel baojiaModel = new IncompleteModel();
            baojiaModel.ItemName = "报价确认中";
            baojiaModel.Count = quoatationCount.ToString();
            baojiaModel.Percentage = totalCount > 0 ? (quoatationCount * 100 / totalIncompleteCount).ToString() : "0";
            models.Add(baojiaModel);

            IncompleteModel totalModel = new IncompleteModel();
            totalModel.ItemName = "合计";
            totalModel.Count = (quoatationCount + dxCount + sjCount + fcCount + bjCount).ToString();
            totalModel.Percentage = totalCount > 0 ? ((quoatationCount + dxCount + sjCount + fcCount + bjCount) * 100 / totalCount).ToString() : "0";
            models.Add(totalModel);

            return models;
        }
        
        /// <summary>
        /// 1，备件先行 里面的已审批待发货里面操作发货 2，备件申请管理里面 操作审批
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public int GetBJStatisfyCount(int year, int month, int day)
        {            
            DateTime endDate = new DateTime(year, month, day, 17, 0, 0);
            DateTime endDate1 = new DateTime(year, month, day, 23, 59, 59);
            DateTime startDate = endDate.AddDays(-1);
            if (endDate.DayOfWeek == DayOfWeek.Monday)
            {
                startDate = startDate.AddDays(-2);
            }
            // 备件先行满足条件的单数
            int bjxxStatisfyCount = GetEntities<ApplicationProduct>().Where(p => 
                p.ApplicationForm.User.ServiceStation == null &&
                p.ApplicationForm.ApplicationType.StartsWith("B") &&
                p.ApplicationForm.ApplicationDateTime > startDate && p.ApplicationForm.ApplicationDateTime < endDate &&
                p.ApplicationForm.SendBackDateTime < endDate1).Count();
            int bjStatisfyCount = GetEntities<SparePartApplyForm>().Where(s => s.ApplyTime > startDate && s.ApplyTime < endDate &&
                s.DealWithTime < endDate1).Count();

            //int bjxxTotalCount = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ApplicationType.Equals("B") &&
            //    p.ApplicationForm.ApplicationDateTime > startDate && p.ApplicationForm.ApplicationDateTime < endDate).Count();
            //int bjTotalCount = GetEntities<SparePartApplyForm>().Where(s => s.ApplyTime > startDate && s.ApplyTime < endDate).Count();
            //if (bjxxTotalCount + bjTotalCount > 0)
            //{
            //    return (bjxxStatisfyCount + bjStatisfyCount) / (bjxxTotalCount + bjTotalCount);
            //}
            //return 0;
            return bjxxStatisfyCount + bjStatisfyCount;
        }

        public int GetBJStatisfyByMonth(int startYear, int startmonth, int endYear, int endMonth)
        {
            DateTime endDate = new DateTime(endYear, endMonth, 1, 17, 0, 0);
            endDate = endDate.AddMonths(1).AddDays(-1);

            DateTime startDate = new DateTime(startYear, startmonth, 1, 17,0,0).AddDays(-1);
            if (startDate.DayOfWeek == DayOfWeek.Monday)
            {
               startDate = startDate.AddDays(-2);
            }
            int bjxxTotalCount = GetEntities<ApplicationProduct>().Where(p => p.ApplicationForm.ApplicationType.StartsWith("B") &&
                p.ApplicationForm.ApplicationDateTime > startDate && p.ApplicationForm.ApplicationDateTime < endDate).Count();
            int bjTotalCount = GetEntities<SparePartApplyForm>().Where(s => s.ApplyTime > startDate && s.ApplyTime < endDate).Count();
            int totalCount = bjxxTotalCount+bjTotalCount;
            int totalStatisfyCount = 0;
            for (DateTime time = startDate; time <= endDate; time = time.AddDays(1))
            {
                totalStatisfyCount += GetBJStatisfyCount(time.Year, time.Month, time.Day);
            }

            if (totalCount > 0)
            {
                if (totalStatisfyCount < totalCount)
                {
                    return totalStatisfyCount * 100 / totalCount;
                }
                else
                {
                    return 100;
                }
            }
            return 0;
        }

        public List<ZBTJModel> GetBJStatisfyByTimeRange(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    model.ZB = GetBJStatisfyByMonth(year, month, year, month);
                    model.Name = string.Format("{0}年{1}月", year, month);
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }

        /// <summary>
        /// 季度备件满足率
        /// </summary>
        /// <param name="startYear"></param>
        /// <param name="endYear"></param>
        /// <returns></returns>
        public List<ZBTJModel> GetQBJStatisfyList(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0} Q1", year);
                q1Model.Month = "Q1";
                q1Model.ZB = GetBJStatisfyByMonth(year, 4, year, 6);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0} Q2", year);
                q2Model.Month = "Q2";
                q2Model.ZB = GetBJStatisfyByMonth(year, 7, year, 9);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0} Q3", year);
                q3Model.Month = "Q3";
                q3Model.ZB = GetBJStatisfyByMonth(year, 10, year, 12);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0} Q4", year);
                q4Model.Month = "Q4";
                q4Model.ZB = GetBJStatisfyByMonth(year + 1, 1, year + 1, 3);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        /// <summary>
        /// 二次返修率
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetSecondRepairRateOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {            
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    string timeString = string.Format("{0}年{1}月", year, month);
                    ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z=>z.ZBTJType.Equals("二次维修率") && z.TimePeriod.Equals(timeString)).FirstOrDefault();
                    model.ZB = zbtjData == null? 0: Convert.ToInt32(zbtjData.ZB);
                    model.Name = timeString;
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }

        public List<ZBTJModel> GetSecondRepariRateOfSeason(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0}年Q1", year);
                ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("二次维修率") && z.TimePeriod.Equals(q1Model.Name)).FirstOrDefault();
                q1Model.Month = "Q1";
                q1Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0}年Q2", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("二次维修率") && z.TimePeriod.Equals(q2Model.Name)).FirstOrDefault();
                q2Model.Month = "Q2";
                q2Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0}年Q3", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("二次维修率") && z.TimePeriod.Equals(q3Model.Name)).FirstOrDefault();
                q3Model.Month = "Q3";
                q3Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0}年Q4", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("二次维修率") && z.TimePeriod.Equals(q4Model.Name)).FirstOrDefault();
                q4Model.Month = "Q4";
                q4Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        /// <summary>
        /// 二次返修率
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetFactoryZQOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    string timeString = string.Format("{0}年{1}月", year, month);
                    ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("供应商周期") && z.TimePeriod.Equals(timeString)).FirstOrDefault();
                    model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                    model.Name = timeString;
                    model.Month = month.ToString() + "月";
                    //model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }


        public List<ZBTJModel> GetFactoryZQOfSeason(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0}年Q1", year);
                ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("供应商周期") && z.TimePeriod.Equals(q1Model.Name)).FirstOrDefault();
                q1Model.Month = "Q1";
                q1Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                //q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0}年Q2", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("供应商周期") && z.TimePeriod.Equals(q2Model.Name)).FirstOrDefault();
                q2Model.Month = "Q2";
                q2Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                //q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0}年Q3", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("供应商周期") && z.TimePeriod.Equals(q3Model.Name)).FirstOrDefault();
                q3Model.Month = "Q3";
                q3Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                //q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0}年Q4", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("供应商周期") && z.TimePeriod.Equals(q4Model.Name)).FirstOrDefault();
                q4Model.Month = "Q4";
                q4Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                //q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        /// <summary>
        /// 平台升级率=当月平台升级到北京维修的数量/当月所有派单数量
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetPlatformUpgradeRateListOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();

            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    string timeString = string.Format("{0}年{1}月", year, month);
                    model.ZB = GetPlatformUpgradeRateOfMonth(year, month, year, month);
                    model.Name = timeString;
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }

        public List<ZBTJModel> GetPlatformUpgradeRateListOfSeason(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0}年Q1", year);                
                q1Model.Month = "Q1";
                q1Model.ZB = GetPlatformUpgradeRateOfMonth(year, 4, year, 6);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0}年Q2", year);                
                q2Model.ZB = GetPlatformUpgradeRateOfMonth(year, 7, year, 9);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0}年Q3", year);               
                q3Model.Month = "Q3";
                q3Model.ZB = GetPlatformUpgradeRateOfMonth(year, 10, year, 12);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0}年Q4", year);                
                q4Model.Month = "Q4";
                q4Model.ZB = GetPlatformUpgradeRateOfMonth(year+1, 1, year+1, 3);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        private int GetPlatformUpgradeRateOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            IQueryable<ApplicationProduct> products = GetEntities<ApplicationProduct>().Where(p => 
                p.ApplicationForm.User.ServiceStation == null &&
                p.ApplicationForm.ApplicationDateTime > startDate &&
                p.ApplicationForm.ApplicationDateTime < endDate);
            int totalUpgradeCount = 0;
            int totalProductCount = 0;
            foreach (ApplicationProduct product in products)
            {
                //if (!product.ApplicationFormReference.IsLoaded)
                //{
                //    product.ApplicationFormReference.Load();
                //}
                //if (!product.ApplicationForm.UserReference.IsLoaded)
                //{
                //    product.ApplicationForm.UserReference.Load();
                //}
                //if (!product.ApplicationForm.User.ServiceStationReference.IsLoaded)
                //{
                //    product.ApplicationForm.User.ServiceStationReference.Load();
                //}
                //if (product.ApplicationForm.User.ServiceStation == null)
                //{
                    totalProductCount++;
                    if (!product.UpgradeForm.IsLoaded)
                    {
                        product.UpgradeForm.Load();
                    }
                    if (product.UpgradeForm != null &&
                        product.UpgradeForm.Count > 0)
                    {
                        totalUpgradeCount++;
                    }
                //}
            }
            if (totalProductCount > 0)
            {
                return totalUpgradeCount * 100 / totalProductCount;
            }
            return 0;
        }

        /// <summary>
        /// 当月DOA数=当月申请DOA数（上月26日至本月25日的）
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetDOACountListOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();

            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    string timeString = string.Format("{0}年{1}月", year, month);
                    model.ZB = GetDOACountOfMonth(year, month, year, month);
                    model.Name = timeString;
                    model.Month = month.ToString() + "月";
                    //model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }

        public List<ZBTJModel> GetDOACountListOfSeason(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0}年Q1", year);
                q1Model.Month = "Q1";
                q1Model.ZB = GetDOACountOfMonth(year, 4, year, 6);
                //q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0}年Q2", year);
                q2Model.ZB = GetDOACountOfMonth(year, 7, year, 9);
                //q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0}年Q3", year);
                q3Model.Month = "Q3";
                q3Model.ZB = GetDOACountOfMonth(year, 10, year, 12);
                //q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0}年Q4", year);
                q4Model.Month = "Q4";
                q4Model.ZB = GetDOACountOfMonth(year + 1, 1, year + 1, 3);
                //q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        public int GetDOACountOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            IQueryable<ApplicationForm> products = GetEntities<ApplicationForm>().Where(a =>
                a.ApplicationCode.StartsWith("D") &&
                a.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate);
            return products.Count();
        }

        /// <summary>
        /// 当月RMA数=当月收货确认的总数量（上月26日至本月25日的）
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetRMACountListOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();

            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    string timeString = string.Format("{0}年{1}月", year, month);
                    model.ZB = GetRMACountOfMonth(year, month, year, month);
                    model.Name = timeString;
                    model.Month = month.ToString() + "月";
                    //model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }

        public List<ZBTJModel> GetRMACountListOfSeason(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0}年Q1", year);
                q1Model.Month = "Q1";
                q1Model.ZB = GetRMACountOfMonth(year, 4, year, 6);
                //q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0}年Q2", year);
                q2Model.ZB = GetRMACountOfMonth(year, 7, year, 9);
                //q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0}年Q3", year);
                q3Model.Month = "Q3";
                q3Model.ZB = GetRMACountOfMonth(year, 10, year, 12);
                //q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0}年Q4", year);
                q4Model.Month = "Q4";
                q4Model.ZB = GetRMACountOfMonth(year + 1, 1, year + 1, 3);
                //q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        public int GetRMACountOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            DateTime startDate = DateTime.Now;
            if (startMonth > 1)
            {
                startDate = new DateTime(startYear, startMonth - 1, 25);
            }
            else
            {
                startDate = new DateTime(startYear - 1, 12, 25);
            }
            DateTime endDate = new DateTime(endYear, endMonth, 26);

            IQueryable<ApplicationForm> products = GetEntities<ApplicationForm>().Where(a =>
                a.ApplicationCode.StartsWith("R") &&
                a.User.ServiceStation == null &&
                a.DistributeDateTime > startDate &&
                a.DistributeDateTime < endDate);
            return products.Count();
        }

        /// <summary>
        /// 备件先行满足率=当天下5点之前申请并发货的数量/当天所有申请的总和*100%
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetBJXXStatisfyListOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    string timeString = string.Format("{0}年{1}月", year, month);
                    ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件先行满足率") && z.TimePeriod.Equals(timeString)).FirstOrDefault();
                    model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                    model.Name = timeString;
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }

        public int GetBJXXStatisfyByMonth(int year, int month)
        {
            string timeString = string.Format("{0}年{1}月", year, month);
            ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件先行满足率") && z.TimePeriod.Equals(timeString)).FirstOrDefault();

            if (zbtjData != null)
            {
                return Convert.ToInt32(zbtjData.ZB);    
            }
            return 0;         
        }

        public List<ZBTJModel> GetBJXXStatisfyOfSeason(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0}年Q1", year);
                ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件先行满足率") && z.TimePeriod.Equals(q1Model.Name)).FirstOrDefault();
                q1Model.Month = "Q1";
                q1Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0}年Q2", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件先行满足率") && z.TimePeriod.Equals(q2Model.Name)).FirstOrDefault();
                q2Model.Month = "Q2";
                q2Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0}年Q3", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件先行满足率") && z.TimePeriod.Equals(q3Model.Name)).FirstOrDefault();
                q3Model.Month = "Q3";
                q3Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0}年Q4", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件先行满足率") && z.TimePeriod.Equals(q4Model.Name)).FirstOrDefault();
                q4Model.Month = "Q4";
                q4Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }

        /// <summary>
        /// 备件清理率=5天从待修流程走的单量/当月所有派单的数量*100%
        /// </summary>
        /// <returns></returns>
        public List<ZBTJModel> GetBJQLListOfMonth(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                int startMonth1 = startMonth;
                int endMonth1 = endMonth;
                if (year != startYear)
                {
                    startMonth1 = 1;
                }
                if (year != endYear)
                {
                    endMonth1 = 12;
                }
                for (int month = startMonth1; month <= endMonth1; month++)
                {
                    ZBTJModel model = new ZBTJModel();
                    string timeString = string.Format("{0}年{1}月", year, month);
                    ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件清理率") && z.TimePeriod.Equals(timeString)).FirstOrDefault();
                    model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                    model.Name = timeString;
                    model.Month = month.ToString() + "月";
                    model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", model.ZB, 100 - model.ZB);
                    models.Add(model);
                }
            }
            return models;
        }        

        public List<ZBTJModel> GetBJQLOfSeason(int startYear, int endYear)
        {
            List<ZBTJModel> models = new List<ZBTJModel>();
            for (int year = startYear; year <= endYear; year++)
            {
                ZBTJModel q1Model = new ZBTJModel();
                q1Model.Name = string.Format("{0}年Q1", year);
                ZBTJData zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件清理率") && z.TimePeriod.Equals(q1Model.Name)).FirstOrDefault();
                q1Model.Month = "Q1";
                q1Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q1Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q1Model.ZB, 100 - q1Model.ZB);
                models.Add(q1Model);

                ZBTJModel q2Model = new ZBTJModel();
                q2Model.Name = string.Format("{0}年Q2", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件清理率") && z.TimePeriod.Equals(q2Model.Name)).FirstOrDefault();
                q2Model.Month = "Q2";
                q2Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q2Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q2Model.ZB, 100 - q2Model.ZB);
                models.Add(q2Model);

                ZBTJModel q3Model = new ZBTJModel();
                q3Model.Name = string.Format("{0}年Q3", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件清理率") && z.TimePeriod.Equals(q3Model.Name)).FirstOrDefault();
                q3Model.Month = "Q3";
                q3Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q3Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q3Model.ZB, 100 - q3Model.ZB);
                models.Add(q3Model);

                ZBTJModel q4Model = new ZBTJModel();
                q4Model.Name = string.Format("{0}年Q4", year);
                zbtjData = GetEntities<ZBTJData>().Where(z => z.ZBTJType.Equals("备件清理率") && z.TimePeriod.Equals(q4Model.Name)).FirstOrDefault();
                q4Model.Month = "Q4";
                q4Model.ZB = zbtjData == null ? 0 : Convert.ToInt32(zbtjData.ZB);
                q4Model.TSURL = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x20&chco=CC0000,FFFF00&chxt=x,y&chxs=0,000000,0,0,_|1,000000,0,0,_&chd=t:{0}|{1}&&chf=bg,s,65432100", q4Model.ZB, 100 - q4Model.ZB);
                models.Add(q4Model);
            }
            return models;
        }
    }
}
